﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

/// <summary>
/// 
/// </summary>
public static class CommonExtenders
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string ToSQLString(this string value)
    {
        if (string.IsNullOrEmpty(value))
        {
            return string.Empty;
        }
        else
        {
            return value.Replace("'", "''");
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string ToSQLString(this bool value)
    {
        if (value)
        {
            return "1";
        }
        else
        {
            return "0";
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static object DbObject(this object value)
    {
        return (value == DBNull.Value ? null : value);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string DbString(this object value)
    {
        return (value == DBNull.Value ? string.Empty : Convert.ToString(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static int DbInt(this object value)
    {
        return (value == DBNull.Value ? 0 : Convert.ToInt32(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static decimal DbDecimal(this object value)
    {
        return (value == DBNull.Value ? 0 : Convert.ToDecimal(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool DbBool(this object value)
    {
        return (value == DBNull.Value ? false : Convert.ToBoolean(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static DateTime DbDate(this object value)
    {
        return (value == DBNull.Value ? DateTime.Now : Convert.ToDateTime(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static DateTime DbDateNullable(this object value)
    {
        return (value == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static double DbDouble(this object value)
    {
        return (value == DBNull.Value ? 0 : Convert.ToDouble(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static long DbLong(this object value)
    {
        return (value == DBNull.Value ? 0 : Convert.ToInt64(value, CultureInfo.InvariantCulture));
    }
}